{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "什么是函数？\n",
    "* 函数是组织好的，可重复使用的，用来实现单一，或相关联功能的代码段。\n",
    "* 函数用def语句定义\n",
    "* 函数用return来返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']\n"
     ]
    }
   ],
   "source": [
    "# 现在我想生成一个有10个字母a的列表\n",
    "lst =[]\n",
    "for i in range(10):\n",
    "    lst.append('a')\n",
    "print(lst)\n",
    "# 也可以这样写：lst = ['a' for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']\n",
      "['helloworld', 'helloworld', 'helloworld', 'helloworld', 'helloworld']\n",
      "[[1, 2, 3], [1, 2, 3], [1, 2, 3]]\n"
     ]
    }
   ],
   "source": [
    "# 那么如果我想做一个功能：生成n个m的列表\n",
    "def f1(n,m):\n",
    "    lstf = []\n",
    "    for i in range(n):\n",
    "        lstf.append(m)\n",
    "    return lstf\n",
    "# 定义好函数，其中有两个参数 → n代表个数，m代表元素值\n",
    "# 同时注意，这里的lstf是局部变量\n",
    "\n",
    "print(f1(10,'a'))\n",
    "print(f1(5,'helloworld'))\n",
    "print(f1(3,[1,2,3]))\n",
    "# 执行函数\n",
    "# 这里要注意return的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 我想创建一个函数，函数的功能就是print出‘hello world’\n",
    "def f2():\n",
    "    print('hello world')\n",
    "# 定义好函数，这里并没有输入参数\n",
    "\n",
    "f2()\n",
    "# 执行函数"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
